/**
 *	Copyright (c) 2009, SpLib.net
 * 	All rights reserved.
  	
 * 	Author:6spring
 * 	About:占用关系
*/

package SpLib.Tools
{
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class Mapbit
	{
		private var m_Data:Array = null;
		
		private var m_nHFrame:int = 0;
		private var m_nVFrame:int = 0;
		private var m_nWidth:int = 0;
				
		public function Mapbit()
		{
		}
		
		public function get width():int
		{
			return m_nWidth;
		}
		
		public function get height():int
		{
			return m_nHFrame;
		}
		
		public function Init(w:int,h:int,initstate:Boolean):void
		{
			m_nWidth = w;
			
			m_nVFrame = w/32;
			if(w%32) ++m_nVFrame;
			
			m_nHFrame = h;
			 
			m_Data = new Array(m_nHFrame*m_nVFrame);
			
			var i:int;
			var data:int = initstate?0xFFFFFFFF:0;
			for(i=0;i<m_Data.length;i++)
			{
				m_Data[i] = data;
			}
		}
		
		public function LoadFromStr(str:String):void
		{
			var data:ByteArray = Base64.decodeByteArray(str);
			data.position = 0;
			data.uncompress();
			data.position = 0;
			data.endian = flash.utils.Endian.LITTLE_ENDIAN;
			
			var w:int = data.readInt();
			var h:int = data.readInt();
			
			m_nWidth = w;
			m_nHFrame = h;
			
			m_nVFrame = w/32;
			if(w%32) ++m_nVFrame;
			 
			m_Data = new Array(m_nHFrame*m_nVFrame);
			
			var i:int;
			for(i=0;i<m_Data.length;i++)
			{
				m_Data[i] = data.readInt();
			}	
		}
		
		public function SaveAsStr():String
		{			
			var data:ByteArray = new ByteArray();
			data.endian = flash.utils.Endian.LITTLE_ENDIAN;
			
			data.writeInt(m_nWidth);
			data.writeInt(m_nHFrame);
			
			var i:int;
			for(i=0;i<m_Data.length;i++)
			{
				data.writeInt( m_Data[i] as int);
			}
			
			data.position = 0;
			data.compress();
			data.position = 0;
			return Base64.encodeByteArray(data);
		}
		
		public function Get(x:int,y:int):Boolean
		{
			var vb:int = x/32;
			var off:int = x%32;
			if(vb>=m_nVFrame || y>=m_nHFrame) 
			{
				return false;
			}
			
			var data:int = m_Data[y*m_nVFrame+vb];
			
			return ((data>>off)&1)==1;
		}
		
		public function Set(x:int,y:int,can:Boolean):void
		{
			var vb:int = x/32;
			var off:int = x%32;
			if(vb>=m_nVFrame || y>=m_nHFrame) 
			{
				return;
			}
			
			var data:int = m_Data[y*m_nVFrame+vb];
			
			if(can)
			{
				m_Data[y*m_nVFrame+vb] = data|(1<<off);
			}
			else
			{
				m_Data[y*m_nVFrame+vb] = data&(~(1<<off));
			}
		}
		
		public function Union(m:Mapbit):void
		{
		}
		
		public function SetRect(rect:Rectangle,can:Boolean):void
		{
			var i:int;
			var j:int;
			for(i=0;i<rect.height;i++)
			{
				for(j=0;j<rect.width;j++)
				{	
					Set(rect.x + j ,rect.y + i*rect.width ,can);
				}
			}
		}
		
		public function GetRect(rect:Rectangle):Boolean
		{
			var i:int;
			var j:int;
			for(i=0;i<rect.height;i++)
			{
				for(j=0;j<rect.width;j++)
				{	
					if(Get(rect.x + j ,rect.y + i*rect.width ))
					{
						return true;
					}
				}
			}
			
			return false;
		}
	}
}